//
//  ControlsManager.m
//  homework
//
//  Created by weiwang on 15/11/10.
//  Copyright © 2015年 zyb. All rights reserved.
//

#import "ControlsManager.h"
#import "ZYBUIDefine.h"
#define BadgeTag 10101
@implementation ControlsManager

+ (UILabel *)getLabelWithFont:(UIFont *)font
                 textAligment:(NSTextAlignment)aligment
                        frame:(CGRect)frame
                    textColor:(UIColor *)color{
    UILabel *label = [[UILabel alloc] initWithFrame:frame];
    [label setBackgroundColor:[UIColor clearColor]];
    [label setFont:font];
    [label setTextAlignment:aligment];
    [label setTextColor:color];
    
    return label;
}

+ (UIBarButtonItem *)createBarButton: (NSString *)text
                     mormalImageName: (NSString *)mormalImageName
                 hightLightImageName: (NSString *)hightLightImageName
                              target: (id)target
                            selector: (SEL)selector
                              locate:(ItemLocate)locate{
    UIButton *button = [ControlsManager createButtonWithText:text mormalImageName:mormalImageName hightLightImageName:hightLightImageName frame:CGRectMake(0, 0, 52, 31) target:target selector:selector];
    UIBarButtonItem *item = [[UIBarButtonItem alloc] initWithCustomView: button];
    [button setTitleEdgeInsets:UIEdgeInsetsMake(0, 0, -1, 0)];
    button.titleLabel.font = kItemTitleFont;
    return item;
}

+ (UIBarButtonItem *)createImageTypeBarButtonWithNormalImageName:(NSString *)mormalImageName
                                             hightLightImageName:(NSString *)hightLightImageName
                                                          target: (id)target
                                                        selector: (SEL)selector
                                                          locate:(ItemLocate)locate{
    UIButton *button = [ControlsManager createButtonWithText:nil mormalImageName:mormalImageName hightLightImageName:hightLightImageName frame:CGRectMake(0, 0, 52, 31) target:target selector:selector];
    UIBarButtonItem *item = [[UIBarButtonItem alloc] initWithCustomView: button];
    [button setTitleEdgeInsets:UIEdgeInsetsMake(0, 0, -1, 0)];
    button.titleLabel.font = kItemTitleFont;
    return item;
}

+ (UIBarButtonItem *)createImageTypeBarButton:(NSString *)mormalImageName
                                       target: (id)target
                                     selector: (SEL)selector
                                       locate:(ItemLocate)locate{
    UIButton *button = [ControlsManager createButtonWithText:nil mormalImageName:mormalImageName hightLightImageName:mormalImageName frame:CGRectMake(0, 0, 52, 31) target:target selector:selector];
    UIBarButtonItem *item = [[UIBarButtonItem alloc] initWithCustomView: button];
    [button setTitleEdgeInsets:UIEdgeInsetsMake(0, 0, -1, 0)];
    button.titleLabel.font = kItemTitleFont;
    return item;
}

+ (UIBarButtonItem *)createTextTypeBarButtonWithText: (NSString *)text
                                              target: (id)target
                                            selector: (SEL)selector
                                              locate:(ItemLocate)locate{ 
    UIButton *button = [ControlsManager createButtonWithText:text mormalImageName:nil hightLightImageName:nil frame:CGRectMake(0, 0, 52, 31) target:target selector:selector];
    UIBarButtonItem *item = [[UIBarButtonItem alloc] initWithCustomView: button];
    [button setTitleEdgeInsets:UIEdgeInsetsMake(0, 0, -1, 0)];
    button.titleLabel.font = kItemTitleFont;
    return item;
}

+ (UIButton *)createButtonWithText:(NSString *)text
                       normalColor:(UIColor*)normalColor
                   hightLightColor:(UIColor *)hightLightColor
                          textFont:(UIFont *)textFont
                       mormalImage:(UIImage *)mormalImage
                   hightLightImage:(UIImage *)hightLightImage
                             frame:(CGRect)frame
                            target:(id)target
                          selector:(SEL)selector{
    NSInteger buttonWidth = 0;
    if ([text stringIsValid])
    {
        CGSize textSize = [text stringSizeWithFont:kButtonTitleFont constrainWidth:CGFLOAT_MAX];
        NSInteger calculateWidth = textSize.width + 20;
        
        buttonWidth = calculateWidth > CGRectGetWidth(frame) ? calculateWidth : CGRectGetWidth(frame);
    }
    else
    {
        buttonWidth = CGRectGetWidth(frame) == 0 ? 52 : CGRectGetWidth(frame);
    }
    frame.size.width = buttonWidth;
    
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    [button setFrame:frame];
    [button setBackgroundColor:normalColor forState:UIControlStateNormal];
    if (hightLightColor) {
        [button setBackgroundColor:hightLightColor forState:UIControlStateHighlighted];
    }
    button.titleLabel.font = textFont;
    [button setTitle:text forState:UIControlStateNormal];
    [button setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];

    [button addTarget:target action:selector forControlEvents:UIControlEventTouchUpInside];
    
    if (mormalImage && hightLightImage){
        [button setBackgroundImage:mormalImage forState:UIControlStateNormal];
        [button setBackgroundImage:hightLightImage forState:UIControlStateHighlighted];
        [button setBackgroundImage:hightLightImage forState:UIControlStateSelected];
    }
    return button;
}


+ (UIButton *)createButtonWithText:(NSString *)text
             mormalImageName: (NSString *)mormalImageName
         hightLightImageName:(NSString *)hightLightImageName
                       frame:(CGRect)frame
                      target:(id)target
                    selector:(SEL)selector{
    NSInteger buttonWidth = 0;
    if ([text stringIsValid])
    {
        CGSize textSize = [text stringSizeWithFont:kButtonTitleFont constrainWidth:CGFLOAT_MAX];
        NSInteger calculateWidth = textSize.width + 20;
        
        buttonWidth = calculateWidth > CGRectGetWidth(frame) ? calculateWidth : CGRectGetWidth(frame);
    }
    else
    {
        buttonWidth = CGRectGetWidth(frame) == 0 ? 52 : CGRectGetWidth(frame);
    }
    frame.size.width = buttonWidth;
    
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    [button setFrame:frame];
    [button setTitle:text forState:UIControlStateNormal];
    if (target && selector) {
        [button addTarget:target action:selector forControlEvents:UIControlEventTouchUpInside];
    }
    [button setTitleEdgeInsets:UIEdgeInsetsMake(2.0f, 0.0f, 0.0f, 0.0f)];
    
    if ([mormalImageName stringIsValid] && [hightLightImageName stringIsValid]){
        [button setBackgroundImage:[UIImage imageNamed:mormalImageName] forState:UIControlStateNormal];
        [button setBackgroundImage:[UIImage imageNamed:hightLightImageName] forState:UIControlStateHighlighted];
        [button setBackgroundImage:[UIImage imageNamed:hightLightImageName] forState:UIControlStateSelected];
    }
    return button;
}

+ (UIButton *)createButtonWithText:(NSString *)text
                   mormalImageName: (NSString *)mormalImageName
               hightLightImageName:(NSString *)hightLightImageName
                             frame:(CGRect)frame{
    NSInteger buttonWidth = 0;
    if ([text stringIsValid])
    {
        CGSize textSize = [text stringSizeWithFont:kButtonTitleFont constrainWidth:CGFLOAT_MAX];
        NSInteger calculateWidth = textSize.width + 20;
        
        buttonWidth = calculateWidth > CGRectGetWidth(frame) ? calculateWidth : CGRectGetWidth(frame);
    }
    else
    {
        buttonWidth = CGRectGetWidth(frame) == 0 ? 52 : CGRectGetWidth(frame);
    }
    frame.size.width = buttonWidth;
    
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    [button setFrame:frame];
    [button setTitle:text forState:UIControlStateNormal];
    [button setTitleEdgeInsets:UIEdgeInsetsMake(2.0f, 0.0f, 0.0f, 0.0f)];
    
    if ([mormalImageName stringIsValid] && [hightLightImageName stringIsValid]){
        [button setBackgroundImage:[UIImage imageNamed:mormalImageName] forState:UIControlStateNormal];
        [button setBackgroundImage:[UIImage imageNamed:hightLightImageName] forState:UIControlStateHighlighted];
        [button setBackgroundImage:[UIImage imageNamed:hightLightImageName] forState:UIControlStateSelected];
    }
    return button;
}

+ (UIBarButtonItem *)barWithImage:(UIImage *)image
                    selectedImage:(UIImage *)selectedImage
                  backgroundImage:(UIImage *)backgroundImage
          selectedBackgroundImage:(UIImage *)selectedBackgroundImage
                           target:(id)target
                           action:(SEL)action
{
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    
    if (image) {
        [button setImage:image forState:UIControlStateNormal];
    }
    if (selectedImage) {
        [button setImage:selectedImage forState:UIControlStateHighlighted];
        [button setImage:selectedImage forState:UIControlStateSelected];
    }
    if (backgroundImage) {
        [button setBackgroundImage:backgroundImage forState:UIControlStateNormal];
    }
    if (selectedBackgroundImage) {
        [button setBackgroundImage:selectedBackgroundImage forState:UIControlStateHighlighted];
        [button setBackgroundImage:selectedBackgroundImage forState:UIControlStateSelected];
    }
    if (target && action) {
        [button addTarget:target action:action forControlEvents:UIControlEventTouchUpInside];
    }
    
    CGFloat width = 44;
    CGFloat height = 44;
    if (backgroundImage && image) {
        [button setShowsTouchWhenHighlighted:NO];
        if (backgroundImage.size.width > (image.size.width + 16)) {
            width = backgroundImage.size.width;
        }
        height = backgroundImage.size.height;
    }else if(image) {
        [button setShowsTouchWhenHighlighted:YES];
        
        if (width < image.size.width) {
            width = image.size.width;
        }
        if (height < image.size.height) {
            height = image.size.height;
        }
    }
    
    
    button.frame = CGRectMake(0, 0, width, height);
    
    UIBarButtonItem *buttonItem = [[UIBarButtonItem alloc] initWithCustomView:button];
    buttonItem.style = UIBarButtonItemStyleBordered;
    return buttonItem;
}


+ (UIButton *)createButtonWithText:(NSString *)text
                       frame:(CGRect)frame
                      target:(id)target
                    selector:(SEL)selector{
    return [ControlsManager createButtonWithText:text mormalImageName:nil hightLightImageName:nil frame:frame target:target selector:selector];
}

+ (UIButton *)createButtonWithNormalImageName:(NSString *)mormalImageName
                    hightLightImageName:(NSString *)hightLightImageName
                                  frame:(CGRect)frame
                                 target:(id)target
                               selector:(SEL)selector{
    return [ControlsManager createButtonWithText:nil mormalImageName:mormalImageName hightLightImageName:hightLightImageName frame:frame target:target selector:selector];

}

+ (UIButton *)createButtonWithImageName:(NSString *)ImageName
                            frame:(CGRect)frame
                           target:(id)target
                         selector:(SEL)selector{
    return [ControlsManager createButtonWithText:nil mormalImageName:ImageName hightLightImageName:ImageName frame:frame target:target selector:selector];
}

+ (UIButton *)createButtonWithButtonImage:(NSString *)imageName
                        selectButtonImage:(NSString *)seleceImageName
                                    frame:(CGRect)frame
                                     text:(NSString *)text
                                textColor:(UIColor *)textColor
                          backgroundColor:(UIColor *)backgroundColor
                                 textFont:(UIFont *)textFont
                                   target:(id)target
                                 selector:(SEL)selector{
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    [button setBackgroundColor:backgroundColor forState:UIControlStateNormal];
    [button setTitleColor:textColor forState:UIControlStateNormal];
    [button.titleLabel setFont:textFont];
    [button setTitle:text forState:UIControlStateNormal];
    [button setImage:[UIImage imageNamed:imageName] forState:UIControlStateHighlighted];
    [button setImage:[UIImage imageNamed:seleceImageName] forState:UIControlStateNormal];
    [button setImageEdgeInsets:UIEdgeInsetsMake(0, -5, 0, 0)];
    [button setTitleEdgeInsets:UIEdgeInsetsMake(0, 0, 0, -5)];
    [button addTarget:target action:selector forControlEvents:UIControlEventTouchUpInside];
    [button setFrame:frame];
    return button;
}

+ (UIButton *)createDefaultBackColorButtonWithButtonImage:(NSString *)imageName
                        selectButtonImage:(NSString *)seleceImageName
                                    frame:(CGRect)frame
                                     text:(NSString *)text
                                 textFont:(UIFont *)textFont
                                   target:(id)target
                                 selector:(SEL)selector{
    return [self createButtonWithButtonImage:imageName selectButtonImage:seleceImageName frame:frame text:text textColor:[UIColor colorWithHex:0xffffff] backgroundColor:[UIColor clearColor] textFont:textFont target:target selector:selector];
}

+ (UIButton *)createDefaultBackColorAndFontButtonWithButtonImage:(NSString *)imageName
                        selectButtonImage:(NSString *)seleceImageName
                                    frame:(CGRect)frame
                                     text:(NSString *)text
                                   target:(id)target
                                 selector:(SEL)selector{
    return [self createButtonWithButtonImage:imageName selectButtonImage:seleceImageName frame:frame text:text textColor:[UIColor whiteColor] backgroundColor:[UIColor clearColor] textFont:[UIFont systemFontOfSize:14.0] target:target selector:selector];
}

+ (UILabel *)createDefaultLabelImageViewWithFrame:(CGRect)frame{
    UILabel *label = [[UILabel alloc] initWithFrame:frame];
    
    label.font = UI_NORMAL_FONT(11);
    label.textColor = [UIColor colorWithHex:0x666666];
//    label.backgroundColor = [UIColor colorWithHex:0xeeeeee];
    [label setBackgroundColor:[UIColor colorWithHex:0xf2f2f2] ];

    label.textAlignment = NSTextAlignmentCenter;
    label.lineBreakMode = NSLineBreakByTruncatingTail;
    label.numberOfLines = 3;
    label.userInteractionEnabled = YES;
    
    return label;
}

+ (UIImageView *)createDefaultBadgeWithString:(NSString *)string{
    UIImageView *imageView = [[UIImageView alloc] init];
    imageView.image = [UIImage imageWithColor:[UIColor colorWithHex:0xff6565]];
    [imageView setFrame:CGRectMake(0, 0, 18, 18)];
    imageView.layer.cornerRadius = imageView.frame.size.height*.5f;
    imageView.layer.masksToBounds = YES;

    if (string.length > 1){
        CGSize size = [string stringSizeWithFont:[UIFont systemFontOfSize:12] constrainWidth:kScreenWidth];
        imageView.width = size.width + 15;
    }
    
    UILabel *label = [[UILabel alloc] initWithFrame:imageView.bounds];
    [label setBackgroundColor:[UIColor clearColor]];
    label.font = [UIFont customFontOfSize:12];
    label.textAlignment = NSTextAlignmentCenter;
    label.textColor = [UIColor whiteColor];
    label.text = string;
    [imageView addSubview:label];
    label.tag = BadgeTag;
    
    return imageView;
}

+ (void)addBadgeViewForView:(UIView *)view
              animationType:(WBadgeAnimType)type
                 badgeStyle:(WBadgeStyle)badgeStyle
              contentOffset:(CGPoint)contentOffset
                      value:(NSInteger)value{
    [self addBadgeViewForView:view animationType:type badgeStyle:badgeStyle contentOffset:contentOffset value:value backgroundColor:[UIColor colorWithHex:0xff6565]];
}

+ (void)addBadgeViewForView:(UIView *)view
              animationType:(WBadgeAnimType)type
                 badgeStyle:(WBadgeStyle)badgeStyle
              contentOffset:(CGPoint)contentOffset
                      value:(NSInteger)value
            backgroundColor:(UIColor *)backColor{
    view.badgeCenterOffset = contentOffset;
    view.badgeBgColor = backColor;
    [view showBadgeWithStyle:badgeStyle value:value animationType:type];
}

+ (void)addBadgeViewForBarItem:(UIBarButtonItem *)view
              animationType:(WBadgeAnimType)type
                 badgeStyle:(WBadgeStyle)badgeStyle
              contentOffset:(CGPoint)contentOffset
                      value:(NSInteger)value{
    [self addBadgeViewForBarItem:view animationType:type badgeStyle:badgeStyle contentOffset:contentOffset value:value backgroundColor:[UIColor colorWithHex:0xff6565]];
}

+ (void)addBadgeViewForBarItem:(UIBarButtonItem *)view
              animationType:(WBadgeAnimType)type
                 badgeStyle:(WBadgeStyle)badgeStyle
              contentOffset:(CGPoint)contentOffset
                      value:(NSInteger)value
            backgroundColor:(UIColor *)backColor{
    view.badgeCenterOffset = contentOffset;
    view.badgeBgColor = backColor;
    [view showBadgeWithStyle:badgeStyle value:value animationType:type];
}

@end
